package cn.stylefeng.guns.modular.clb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.BusinessException;
import cn.stylefeng.guns.modular.clb.entity.Clb;
import cn.stylefeng.guns.modular.clb.enums.ClbExceptionEnum;
import cn.stylefeng.guns.modular.clb.mapper.ClbMapper;
import cn.stylefeng.guns.modular.clb.pojo.request.ClbRequest;
import cn.stylefeng.guns.modular.clb.service.ClbService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 车辆表业务实现层
 *
 * @author zhu
 * @date 2021/10/06 11:12
 */
@Service
public class ClbServiceImpl extends ServiceImpl<ClbMapper, Clb> implements ClbService {

	@Override
    public void add(ClbRequest clbRequest) {
        Clb clb = new Clb();
        BeanUtil.copyProperties(clbRequest, clb);
        this.save(clb);
    }

    @Override
    public void del(ClbRequest clbRequest) {
        Clb clb = this.queryClb(clbRequest);
        this.removeById(clb.getId());
    }

    @Override
    public void edit(ClbRequest clbRequest) {
        Clb clb = this.queryClb(clbRequest);
        BeanUtil.copyProperties(clbRequest, clb);
        this.updateById(clb);
    }

    @Override
    public Clb detail(ClbRequest clbRequest) {
        return this.queryClb(clbRequest);
    }

    @Override
    public PageResult<Clb> findPage(ClbRequest clbRequest) {
        LambdaQueryWrapper<Clb> wrapper = createWrapper(clbRequest);
        Page<Clb> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<Clb> findList(ClbRequest clbRequest) {
        LambdaQueryWrapper<Clb> wrapper = this.createWrapper(clbRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author zhu
     * @date 2021/10/06 11:12
     */
    private Clb queryClb(ClbRequest clbRequest) {
        Clb clb = this.getById(clbRequest.getId());
        if (ObjectUtil.isEmpty(clb)) {
            throw new BusinessException(ClbExceptionEnum.CLB_NOT_EXISTED);
        }
        return clb;
    }

    /**
     * 创建查询wrapper
     *
     * @author zhu
     * @date 2021/10/06 11:12
     */
    private LambdaQueryWrapper<Clb> createWrapper(ClbRequest clbRequest) {
        LambdaQueryWrapper<Clb> queryWrapper = new LambdaQueryWrapper<>();

        Long id = clbRequest.getId();
        String bbkh = clbRequest.getBbkh();
        String cpzh = clbRequest.getCpzh();
        String czxm = clbRequest.getCzxm();
        Integer zwgs = clbRequest.getZwgs();
        String cldc = clbRequest.getCldc();
        String clzt = clbRequest.getClzt();
        String syzw = clbRequest.getSyzw();
        String tbyy = clbRequest.getTbyy();
        String acount = clbRequest.getAcount();
        String xlbXlbh = clbRequest.getXlbXlbh();
        String gsbGsbh = clbRequest.getGsbGsbh();
        String createTime = clbRequest.getCreateTime();
        Long createUser = clbRequest.getCreateUser();
        String updateTime = clbRequest.getUpdateTime();
        Long updateUser = clbRequest.getUpdateUser();

        queryWrapper.eq(ObjectUtil.isNotNull(id), Clb::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(bbkh), Clb::getBbkh, bbkh);
        queryWrapper.like(ObjectUtil.isNotEmpty(cpzh), Clb::getCpzh, cpzh);
        queryWrapper.like(ObjectUtil.isNotEmpty(czxm), Clb::getCzxm, czxm);
        queryWrapper.eq(ObjectUtil.isNotNull(zwgs), Clb::getZwgs, zwgs);
        queryWrapper.like(ObjectUtil.isNotEmpty(cldc), Clb::getCldc, cldc);
        queryWrapper.like(ObjectUtil.isNotEmpty(clzt), Clb::getClzt, clzt);
        queryWrapper.like(ObjectUtil.isNotEmpty(syzw), Clb::getSyzw, syzw);
        queryWrapper.like(ObjectUtil.isNotEmpty(tbyy), Clb::getTbyy, tbyy);
        queryWrapper.like(ObjectUtil.isNotEmpty(acount), Clb::getAcount, acount);
        queryWrapper.like(ObjectUtil.isNotEmpty(xlbXlbh), Clb::getXlbXlbh, xlbXlbh);
        queryWrapper.like(ObjectUtil.isNotEmpty(gsbGsbh), Clb::getGsbGsbh, gsbGsbh);
        queryWrapper.eq(ObjectUtil.isNotNull(createTime), Clb::getCreateTime, createTime);
        queryWrapper.eq(ObjectUtil.isNotNull(createUser), Clb::getCreateUser, createUser);
        queryWrapper.eq(ObjectUtil.isNotNull(updateTime), Clb::getUpdateTime, updateTime);
        queryWrapper.eq(ObjectUtil.isNotNull(updateUser), Clb::getUpdateUser, updateUser);

        return queryWrapper;
    }

}